package com.sfd.message.remind.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfd.message.remind.constant.enums.FestivalDefaultType;
import com.sfd.message.remind.entity.FestivalDefault;
import com.sfd.message.remind.mapper.FestivalDefaultMapper;
import com.sfd.message.remind.module.DayList;
import com.sfd.message.remind.module.Result;
import com.sfd.message.remind.service.FestivalDefaultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author haden
 * @date 2022/9/7 15:29
 **/
@Service
@Slf4j
public class FestivalDefaultServiceImpl extends ServiceImpl<FestivalDefaultMapper, FestivalDefault> implements FestivalDefaultService {

    @Override
    public Result<String> defaultFestival() {
        //新历数据
        DayList day = new DayList();
        day.add("01-01", "元旦", "02-14", "情人节", "03-08", "三八妇女节", "05-01", "五一劳动节", "05-04", "五四青年节", "06-01", "六一儿童节", "07-01", "建党节", "08-01", "八一建军节");
        day.add("09-10", "教师节", "10-01", "国庆节", "12-20", "澳门回归纪念日", "12-24", "感恩节", "12-25", "圣诞节");
        List<FestivalDefault> pubDefault = day.getList();
        log.info(JSONObject.toJSONString(pubDefault));
        //农历数据
        DayList chineDay = new DayList();
        chineDay.addChine("01-01", "春节").addChine("01-15", "元宵节").addChine("05-05", "端午节").addChine("07-07", "七夕节").addChine("07-15", "中元节", "08-15", "中秋节", "09-09", "重阳节");
        List<FestivalDefault> chineDefault = chineDay.getList();
        //清理旧数据
        List<Integer> types = Arrays.stream(FestivalDefaultType.values()).map(FestivalDefaultType::getKey).collect(Collectors.toList());
        log.info(JSONObject.toJSONString(types));
        LambdaQueryWrapper<FestivalDefault> wrapper = Wrappers.lambdaQuery();
        wrapper.in(FestivalDefault::getType, types);
        baseMapper.delete(wrapper);
        //存库
        saveBatch(pubDefault);
        saveBatch(chineDefault);
        return Result.success();
    }

    @Override
    public List<FestivalDefault> getByType(Integer type) {
        return lambdaQuery().eq(FestivalDefault::getType, type).list();
    }
}
